angular.module('BaseServer')

.service('BaseClass', ['BaseEnum', function(BaseEnum){
    return {
        Round: function(precision, roundType, roundValue){
            this.Precision = 0;
            this.RoundType = BaseEnum.RoundType.AccondValueRound.Value;
            this.RoundValue = 5;

            if (precision != undefined) {
                this.Precision = precision;
            }
            if (roundType != undefined) {
                this.RroundType = roundType;
            }
            if (roundValue != undefined) {
                this.RoundValue = roundValue;
            }

            this.formatNumber = function(val) {
                var returnValue;

                if(val == undefined || val == null || val.toString().trim() == ''){
                    return '';
                }
                if (this.RoundType == BaseEnum.RoundType.AllAdd.Value) {
                    returnValue = parseInt(val * Math.pow(10, this.Precision + 1));

                    if (returnValue % 10 > 0) {
                        returnValue = parseInt(val * Math.pow(10, this.Precision)) + 1;
                    }

                    returnValue = returnValue / Math.pow(10, this.Precision);
                }
                else if (this.RoundType == BaseEnum.RoundType.AllDelete.Value) {
                    returnValue = parseInt(val * Math.pow(10, this.Precision)) / Math.pow(10, this.Precision);
                }
                else {
                    returnValue = parseInt(val * Math.pow(10, this.Precision + 1));

                    if (returnValue % 10 >= this.RoundValue) {
                        returnValue = parseInt(val * Math.pow(10, this.Precision)) + 1;
                    }
                    else {
                        returnValue = parseInt(returnValue / 10);
                    }
                    returnValue = returnValue / Math.pow(10, this.Precision);
                }

                return parseFloat(returnValue).toFixed(this.Precision);
            };

            this.formatPercent = function(val){
                if(val == undefined || val == null || val.toString().trim() == ''){
                    return '';
                }

                return this.formatNumber(val * 100).toString() + "%";
            };
        }
    };
}]);

